package com.whz.generic.threads;

import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.BeanFactory;
import org.springframework.context.event.ApplicationEventMulticaster;
import org.springframework.context.event.SimpleApplicationEventMulticaster;

import java.util.concurrent.ExecutorService;

/**
 * 配置全局{@link ApplicationEventMulticaster}
 *
 * @author whz
 */
@Slf4j
public class GenericSpringEventMultiCaster {

    public ApplicationEventMulticaster tongyuEventMulticaster(BeanFactory beanFactory, ExecutorService service) {
        SimpleApplicationEventMulticaster simple =
                new SimpleApplicationEventMulticaster(beanFactory);

        simple.setErrorHandler(
                t -> log.error("application event multicaster throw errors.", t));
        simple.setTaskExecutor(service);

        return simple;
    }
}